<template>
    <div class="selectAnchor">
        <van-nav-bar title="选择电商部门" left-arrow @click-left="handleLeft">
        </van-nav-bar>
        <div class="product_list_serch-text">
            <van-search class="product_list_serch1" v-model="queryParams.coreName" shape="round" background="#F6FAFF"
                placeholder="请输入部门名称" />
            <van-button class="product_list-btn" round @click="onSearch">搜索</van-button>
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无电商部门</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div @click="storageTypeClcik(index, item)"
                        :class="storageTypeIndex == index ? 'storageType_item_active' : 'storageType_item'"
                        v-for="(item, index) in list" :key="index">
                        {{ item.deptName }}
                        <van-image :src="xuanzhong" v-if="storageTypeIndex == index"></van-image>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>
    </div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { getOutDepByName } from '@/api/productHome/index'
const router = useRouter();
const route = useRoute();
const xuanzhong = new URL("@/assets/productHome/icon_xuanzhong.png", import.meta.url).href;
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href

// 入口 主播选品出库 == 1 退还供应商出库 == 2
const indexs: any = computed(() => {
    return route.query.index;
});

/** 商品详情传过来的商品 */
const detailInfo = computed(() => {
    return route.query.detailInfo;
});

// 限制连续刷新数据重复问题
const refreFlag = ref(true)
const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any>([])
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    // 搜索值
    coreName: '',
    isOutStock: 0
});
const storageTypeIndex = ref()
const storageTypeClcik = (index: number, item: any) => {
    storageTypeIndex.value = index
    // 给个选中得延迟
    setTimeout(() => {
        router.push({
            path: '/addOutbound',
            query: {
                coreId: item.deptId,
                index: indexs.value,
                detailInfo: detailInfo.value
            }
        })
    }, 200)
}
/** 回退 */
const handleLeft = () => {
    router.push('/outboundType')
}
/** 搜索 */
const onSearch = () => {
    onRefresh()
}
/** 查询主播列表 */
const getList = async () => {
    var res: any;
    // 主播选品出库
    if (indexs.value == 0) {
        res = await getOutDepByName({ ...queryParams.value, deptName: '运营部门' });
    } else { // 审核部门出库
        res = await getOutDepByName({ ...queryParams.value, deptName: '审核部门' });
    }
    refreFlag.value = true
    // 空数据 停止加载
    if (res.rows.length == 0) {
        list.value = [];
        finished.value = true;
        return;
    }
    // 有数据 赋值列表 加载状态结束
    res.rows.forEach((item) => {
        finished.value = false;
        list.value.push(item);
    });
    loading.value = false;
    refreshing.value = false;
    // 如果list长度大于等于总数据条数，数据全部加载完成
    if (list.value.length >= res.total) {
        finished.value = true; //结束加载
    }
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
</script>
<style lang="less" scoped>
.selectAnchor {
    height: calc(100vh - 44px);
    width: 100%;
    background-color: #fff;

    .product_list_serch-text {
        display: flex;
        align-items: center;
        width: 100%;
        background: #f6faff;
        height: 34px;
        margin-bottom: 10px;
        padding-top: 10px;
        padding-bottom: 10px;
        position: relative;

        :deep(.van-search__field) {
            background-color: #fff;
            border-radius: 20px;
            padding-left: 10px;
        }

        .product_list_serch1 {
            display: flex;
            align-items: center;
            background: #f6faff;
            width: 100%;
            height: 34px;
            position: relative;

            :deep(.van-search__content) {
                height: 35px;
                padding-left: 0 !important;
            }

            :deep(.van-search__field) {
                height: 35px;
            }
        }

        .product_list-btn {
            width: 54px;
            height: 26px;
            font-size: 14px;
            color: #ffffff;
            border: none;
            background: #0094ff;
            white-space: nowrap;
            position: absolute;
            top: 14px;
            right: 5%;
        }
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }

    }

    :deep(.van-pull-refresh) {
        height: calc(100vh - 144px);
        overflow: scroll;
    }

    .van-image {
        width: 15px;
        height: 15px;
    }

    .storageType_item {
        width: 100%;
        height: 56px;
        box-sizing: border-box;
        padding: 0 15px;
        background-color: #fff;
        font-weight: 400;
        font-size: 16px;
        color: #1A1B1C;
        line-height: 56px;
    }

    .storageType_item_active {
        width: 100%;
        height: 56px;
        box-sizing: border-box;
        padding: 0 15px;
        background: #F6FAFF;
        font-weight: 400;
        font-size: 16px;
        color: #0094FF;
        line-height: 56px;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>